package com.tnsw.facebookautoliker.helper.command;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Context;

import com.facebook.HttpMethod;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Settings;
import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.database.RecordBase;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.database.FriendsList;
import com.tnsw.facebookautoliker.helper.FacebookCommandObject;
import com.tnsw.facebookautoliker.helper.GeneralManager;
import com.tnsw.facebookautoliker.services.AutoLikerService;

public class GetOtherInfo extends FacebookCommandObject implements Request.Callback {

	private String id;
	private DBManager dbm;
	private GeneralManager gm;
	private String signature;
	
	public GetOtherInfo(Context context, String id) {
		super(context);
		this.tag = "GetOtherInfo";
		this.id = id;
		this.signature = this.getClass().getName() + "_" + id;
		
		this.dbm = DBManager.getInstance(context);
		this.gm = GeneralManager.getInstance();
	}

	
	@Override
	protected void handleResult(Response response) {
		super.handleResult(response);
		
		try {
			if (response.getError() == null) {
				MyLog.d(tag, "json_getotherinfo=" + response.getGraphObject().getInnerJSONObject().toString());
				FriendsList fl = new FriendsList();
				fl.primarykey = this.dbm.getNewPrimarykey();
				
				JSONObject jo = response.getGraphObject().getInnerJSONObject();
				if (jo.has("name")) {
					fl.name = jo.getString("name");
				} else if (jo.has("cover")) {
					JSONObject jCover = jo.getJSONObject("cover");
					
					if (jCover.has("name")) {
						fl.name = jCover.getString("name");
					}
				}
				
				fl.id = this.id;
				fl.photo_path = "";
				fl.created = System.currentTimeMillis();
				fl.lastupd = System.currentTimeMillis();
				fl.following = "N";
				fl.is_friend = "N";
				
				
				ArrayList<RecordBase> a = this.dbm.getRecords(FriendsList.class.getName(), "select primarykey from friends_list where id=?", new String[] {this.id}, 0);
				if (a == null) {
					AutoLikerService aService = AutoLikerService.getInstance();
					if (aService != null) {
						aService.updateFriendsList(fl.id, fl);
						MyLog.d(tag, "updated fl.id=" + fl.id + ", name=" + fl.name + " into AutoLikerService");
					}
					
					this.dbm.insertRecord("friends_list", fl, null, true);
					MyLog.d(tag, "inserted new friends_list, id=" + fl.id + ", name=" + fl.name);
										
					GetPhoto gp = new GetPhoto(this.context, fl.id, fl.name);
					this.gm.addNewQueue(gp, 2);

				}
				
			}			
		} catch (Exception e) {
			MyLog.e(tag, "handleResult - " + e.toString(), e);
		}
		
		this.fbm.updateCommandStatus(this, 200, null);
	}
	
	@Override
	public void execute() {
		super.execute();
		
		if (MyLog.isProduction == false) {
			Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
		}
		
		Session session = Session.openActiveSessionFromCache(this.context);
		if (session != null) {
			String path = "/" + this.id;

			Request req = new Request(session, path, null, HttpMethod.GET);
			req.setCallback(this);
			this.standardExecute(req);	
		}
		
		
	}
	
	@Override
	public void onCompleted(Response response) {
		this.handleResult(response);
	}


	@Override
	public String getSignature() {
		return(this.signature);
	}
}
